﻿#include "FindFundNameThread.h"
#include<QDebug>
FindFundNameThread::FindFundNameThread( QObject* parent) 
    :QThread(parent)
{
    // 线程结束后，自动销毁
    connect(this, &FindFundNameThread::finished, this, &FindFundNameThread::deleteLater);
}

void FindFundNameThread::run()
{
    m_fun();
}

const bool FindFundNameThread::isNullptr(FindFund* FindFund)
{
    if (FindFund == nullptr)
    {
        qWarning() << "m_FindFund指针nullptr";
        return true;
    }
    return false;
}

void FindFundNameThread::FindFundName(FindFund* FindFund)
{
    m_fun = bind([=]() {
        if (isNullptr(FindFund))
            return;
        int nValue = FindFund->FindFundName();
        emit ret(nValue);
        });
}

void FindFundNameThread::FindHistoryOne(FindFund* FindFund,const int code, const int Size)
{
    m_fun = bind([=]() {
        if (isNullptr(FindFund))
            return;
    //qInfo() << "查询历史中";
        int nValue = FindFund->FindHistoryOne(code, Size);
        emit ret(nValue);
        });
}

void FindFundNameThread::FindAllHistoryAll(FindFund* FindFund, const int Size)
{
    m_fun = bind([=]() {
        if (isNullptr(FindFund))
        return;
    for (auto&data: FindFund->FundForm())
    {
        int code = data.first;
        emit FindCode(code);
        int nValue = FindFund->FindHistoryOne(code, Size);
        //qInfo() << nValue;
        emit ret(code);
    }
        });
}

void FindFundNameThread::FindAllHistoryWriteAll(FindFund* FindFund, const QString& path, const int Size)
{
    m_fun = bind([=]() {
        if (isNullptr(FindFund))
        return;
    for (auto& data : FindFund->FundForm())
    {
        int code = data.first;
        emit FindCode(code);
        int nValue = FindFund->FindHistoryOne(code, Size);
        emit retCodeAndCount(code, nValue);
        nValue = FindFund->WriteHistoryOne(code,path);
        QString FilePath = QString("%1/%2.csv").arg(path).arg(FindFund->FindCode(code));
        emit retCodeAndCountAndFileName(code, nValue,FilePath);
    }
        });
}

void FindFundNameThread::WriteHistoryAll(FindFund* FindFund, QList<int> code, const QString& path)
{
    m_fun = bind([=]() {
        if (isNullptr(FindFund))
        return;
    for (auto& Code:code)
    {
        emit FindCode(Code);
        int nValue = FindFund->WriteHistoryOne(Code,path);
        emit retCodeAndCount(Code,nValue);
    }
    
        });
}

void FindFundNameThread::ReadHistoryAll(FindFund* FindFund, QFileInfoList file)
{
    m_fun = bind([=]() {
        if (isNullptr(FindFund))
        return;
    for (auto& fileInfo : file)
    {
        emit FindCode(fileInfo.baseName().toInt());
        int nValue = FindFund->ReadHistoryOne(fileInfo.absoluteFilePath());
        emit retCodeAndCountAndFileName(fileInfo.baseName().toInt(), nValue,fileInfo.absoluteFilePath());
    }
        });
}

